import urlConfig from './config.js'
import * as funs from "@/utils/methods-collection/getFun.js"  

const request = {}
const headers = {}

request.globalRequest = (url, method, data, power) => {
		// 权限判断 因为有的接口请求头可能需要添加的参数不一样
	    // 1 ==自定义请求头 (例如通过token校验的接口)
	    // 2 == 头部数据为json
	    // 默认头部数据为键值对
	switch (power) {
		case 1:
			var keys = uni.getStorageSync('token');
			//字段看怎么规定
			headers['Authorization'] = keys; 
			headers['content-type'] = 'application/x-www-form-urlencoded'
			break;
		case 2:
			headers['content-type'] = 'application/x-www-form-urlencoded'
			break;
		default:
			headers['content-type'] = 'application/json'
			
			break;
	}
	return uni.request({
		url: urlConfig + url,
		method,
		data: data,
		dataType: 'JSON',
		header: headers
	}).then(res => {
			let dd = res[1].data;//res.data.status
			if (dd.code == 200) {
				return dd
			} else {
				throw dd
			}
	}).catch(parmas => {
		console.log("catch里的参数parmas==========",parmas);
		if (parmas.code) {
			switch (parmas.code) {
				case 20003:
					
					break;
				default:
					funs.show(parmas.message,0)
					return Promise.reject()
					break;
			}
		} else {
			funs.show('数据获取失败，请确认网络',0)
			
			return Promise.reject()
		}
		
	})
	
	
	
}

export default request